ReplaceText
ReplaceText
Replace indicated text with specified substitution Handle baseText ; a handle to the text to be searched Handle substitutionText ; a handle to the new text Str15 key ; string to substitute
returns a short; a positive value indicating the number of
substitutions performed or a negative value
indicating an error
ReplaceText searches the text specified by the baseText parameter for instances of the key string and replaces each instance with the text indicated
by the substitutionText parameter. The key parameter contains a string to be
used as the substitution marker. Although the substitution text may contain the
key string, the text is inserted verbatim into the base text, and no recursive
substitution occurs.
Returns: a positive value indicating the number of substitutions performed or a negative value indicating an error. The constant noErr is returned
if there is no error or no substitutions performed.The following are
these errors by using the following constants, they have these
specific meanings:
Result codes
(special meanings of Memory Manager errors returned by ReplaceText) memFullErr (108) SetHandleSize fails on baseText
nilHandleErr (109) GetHandleSize fails on baseText or substitutionText
memWZErr (-111) GetHandleSize fails on baseText or substitutionText
assumes that you have Str255 strings containing base text and substitution text and that you want the result to fit in a specified number of pixels.
// Substituting and truncating text
// Assuming inclusion of
#include < string.h>
# define maxInt 32767;
void SubAndTruncText (void);
void DoError (OSErr myErr);
void SubAndTruncText ()
{
Str15 keyStr;
long sizeL;
short myWidth;
short length;
short result;
strcpy ((char *) baseString, (char *)
"\pabcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz");
// insert into baseString...
strcpy ((char *) subsString, (char *) "\pKILROY WAS HERE");
strcpy ((char *) keyStr, (char *)"\pmnop");
// ...in place of this sequence
myWidth = 500; [TOKEN:12079] ...and truncate with this width
sizeL = baseString[0];
myErr = PtrToHand(&baseString[1], & baseHandle, sizeL); if (myErr)
DoError(myErr);
sizeL = subsString[0];
myErr = PtrToHand(&subsString[1], &subsHandle, sizeL); if (myErr)
DoError(myErr);
if (result < 0)
DoError( result);
if(MemErr)
DoError(MemErr);
length = sizeL;
if (MemErr)
DoError(MemErr); [TOKEN:12079] oops, a Mem Mgr error
result = TruncText(myWidth, (* baseHandle), & length, smTruncEnd); if (result < 0)
DoError( result);
if (MemErr)
DoError(myErr); [TOKEN:12079] oops, a Mem Mgr error
}